// import QtQuick 1.0 // to target S60 5th Edition or Maemo 5
import QtQuick 1.1

Item{
    id: _root
    visible: false

    MouseArea{
        anchors.fill: parent
        acceptedButtons: Qt.LeftButton | Qt.RightButton | Qt.MiddleButton
        hoverEnabled: true
    }

    //ダイアログの本文
    property alias dialogTitle: _dlg.dialogTitle
    property alias dialogMessage: _text.text
    property alias visibleCancel: _buttonCancel.visible

    signal clickOkButton        //OKボタンのイベント
    signal clickCancelButton    //cancelボタンのイベント

    onVisibleChanged: {
        if(visible === true){
            //表示された
        }
    }

    DialogBase {
        id: _dlg
        width: calcWidth()
        height: calcHeight()
        anchors.centerIn: parent

        // 表示された時の処理
        function viewEvent(){
            _buttonOk.forceActiveFocus();
        }

        // 幅の計算
        function calcWidth(){
            var btn_width = _areaButton.width * (visibleCancel ? 1 : 2);
            var txt_width = _text.paintedWidth;
            var parent_width = _dlg.parent.width;
            var temp_width = 0;

            if(txt_width < btn_width){
                temp_width = btn_width + 20;
            }else{
                temp_width = txt_width + 20;
            }
            if(temp_width < parent_width){
                // そのまま採用
            }else{
                temp_width = parent_width - border.width * 2;
            }
            return temp_width;
        }
        // 高さの計算
        function calcHeight(){
            var temp_height = titleHeight + _item.height + 10 + 10;
            var parent_height = _dlg.parent.height;
            if(temp_height < parent_height){
                // そのまま
            }else{
                temp_height = parent_height - border.width * 2;
            }
            return temp_height;
        }

        Column {
            id: _item
            anchors.top: _dlg.titleBottom
            anchors.right: _dlg.right
            anchors.left: _dlg.left
            anchors.margins: 10
            spacing: 10

            Text {
                id: _text
                x: 0
                y: 0
                text: "message"
                wrapMode: Text.WordWrap

                font.pointSize: 12
    //            font.pixelSize: _Setting.fontPixelSizeDlgMessageS * density
                color: "#ffffff"
            }

            Row {
                id: _areaButton
                anchors.horizontalCenter: _item.horizontalCenter

                spacing: 5

                Button {
                    id: _buttonOk
                    width: _buttonCancel.width
                    text: "O K"
                    font.pointSize: 14
    //                font.pixelSize: _Setting.fontPixelSizeButton * _dlg.density

                    onClicked : {
                        _root.clickOkButton();
                    }

                    Keys.onTabPressed: {
                        if(_buttonCancel.visible){
                            _buttonCancel.forceActiveFocus();
                        }
                    }
                    Keys.onPressed: {
                        switch(event.key){
                        case Qt.Key_Escape:
                            _root.clickCancelButton();
                            break;
                        case Qt.Key_Enter:
                        case Qt.Key_Return:
                            _root.clickOkButton();
                            break;
                        default:
                            break;
                        }
                    }
                }

                Button {
                    id: _buttonCancel
                    text: "Cancel"
                    font.pointSize: 14
    //                font.pixelSize: _Setting.fontPixelSizeButton * _dlg.density

                    onClicked : {
                        _root.clickCancelButton();
                    }

                    Keys.onTabPressed: {
                        _buttonOk.forceActiveFocus();
                    }
                    Keys.onPressed: {
                        switch(event.key){
                        case Qt.Key_Escape:
                        case Qt.Key_Enter:
                        case Qt.Key_Return:
                            _root.clickCancelButton();
                            break;
                        default:
                            break;
                        }
                    }
                }
            }

        }
    }
}
